/*
 * Account.java
 *
 * Created on 1 novembre 2007, 12:11
 */
package com.reimscopie.beans.entities ;

import java.util.* ;

import javax.persistence.* ;

import com.reimscopie.* ;


/**
 * This entity bean stores data relative to a client account.
 */
@Entity
@NamedQuery(name="get-login",query="SELECT o FROM AccountBean o WHERE o.login = ?1")
public class AccountBean extends ValueBean<Account> {
  
  // personal datas
  private String login ;
  private String password ;
  private String mail ;
  private boolean newsletter ;
  private float balance ;
  
  // more personal info
  private PersonalInfoBean personalInfo ;
  
  // order list
  private Collection<OrderBean> orders ;
  
  
  /**
   * Create a new instance of <code>AccountBean</code> (default constructor).
   */
  public AccountBean() {
    
    orders = new ArrayList<OrderBean>() ;
    
  }
  
  
  /**
   * Create a new instance of <code>AccountBean</code> with all attributes.
   *
   * @param account The valued account.
   */
  public AccountBean(Account account) {
    
    this() ;
    
    if (account.hasId())
      throw new IllegalArgumentException("account has an image") ;
    
    this.login      = account.getLogin() ;
    this.password   = account.getPassword() ;
    this.mail       = account.getMail() ;
    this.newsletter = account.getNewsLetter() ;
    this.balance    = account.getBalance() ;
    
  }
  
  
  /**
   * Return the login name of this account.
   *
   * @return The account login.
   */
  public String getLogin() {
    return login ;
  }


  /**
   * Sets the login name of this account.
   *
   * @param login The new login name.
   */
  public void setLogin(String login) {
    this.login = login ;
  }
  
  
  /**
   * Return the password of this account.
   *
   * @return The password.
   */
  public String getPassword() {
    return password ;
  }
  
  
  /**
   * Sets the password of this account.
   *
   * @param password The new account password.
   */
  public void setPassword(String password) {
    this.password = password ;
  }
  
  
  /**
   * Return the mail address of the account.
   *
   * @return The account mail.
   */
  public String getMail() {
    return mail ;
  }
  
  
  /**
   * Set the mail address of the account.
   *
   * @param mail The new mail address.
   */
  public void setMail(String mail) {
    this.mail = mail ;
  }
  
  /**
   * Return the newsletter status of this account.
   *
   * @return The newsletter status of this account.
   */
  public boolean getNewsLetter() {
    return newsletter ;
  }
  
  
  /**
   * Set the newsletter status of this account.
   *
   * @param newsletter The newsletter status of this account.
   */
  public void setNewsLetter(boolean newsletter) {
    this.newsletter = newsletter ;
  }
  
  
  /**
   * Returns the current balance of the account.
   *
   * @return The account balance (money amount).
   */
  public float getBalance() {
    return balance ;
  }
  
  
  /**
   * Set the balance of this account.
   *
   * @param balance The new balance (money amount).
   */
  public void setBalance(float balance) {
    this.balance = balance ;
  }
  
  
  /**
   * Returns the personnal infos for this account.
   *
   * @return The personnal infos.
   */
  @OneToOne(optional=false)
  public PersonalInfoBean getPersonalInfo() {
    return personalInfo ;
  }
  
  
  /**
   * Set the personal info of this account.
   *
   * @param personalInfo The new personal info.
   */
  public void setPersonalInfo(PersonalInfoBean personalInfo) {
    this.personalInfo = personalInfo ;
  }
  
  
  /**
   * Return the order list of this account.
   *
   * @return All orders.
   */
  @OneToMany
  public Collection<OrderBean> getOrders() {
    return orders ;
  }
  
  
  /**
   * Set the collection of orders made by this user.
   *
   * @param orders The new order list.
   */
  public void setOrders(Collection<OrderBean> orders) {
    this.orders = orders ;
  }
  
  
////////////////////////////////////////////////////////////////////////////////
//                           TRANSFOMATION                                    //
////////////////////////////////////////////////////////////////////////////////

  /**
   * Transforms this entity bean into the associated product.
   *
   * @return A new product instance.
   *
   * @see #toValue
   */
  public Account toAccount() {
    return toValue() ;
  }

  
  /**
   * Transform this entity bean into the associated value.
   * 
   * @return A new value instance.
   */
  @Override
  public Account toValue() {
    
    return new Account(
        getId(),
        getLogin(),
        getPassword(),
        getMail(),
        getNewsLetter(),
        getBalance(),
        getPersonalInfo().toPersonalInfo()
    ) ;
    
  }
  
}
